<?php
namespace Qy {
/**
* 生成发送消息类
* @author Jamers
* @since 2016.12.31
* @see http://qydev.weixin.qq.com/wiki/index.php?title=%E8%A2%AB%E5%8A%A8%E5%93%8D%E5%BA%94%E6%B6%88%E6%81%AF
* 
* @example 这是特殊的news生成方式
$msg = new MessageBody();
$a = array(
    'to' => 'Jamers',
    'from' => 'zomew',
    'items' => array(
        array(
            'title' => '标题1',
            'description' => '描述1',
            'picurl' => 'picurl 1',
            'url' => 'url 1',
        ),
        array(
            'title' => '标题2',
            'description' => '描述2',
            'picurl' => 'picurl 12',
            'url' => 'url 21',
        ),
    ),
);

$b = $msg->GeneralMessage($a,'news');
echo "<textarea>";
echo $b;
echo "</textarea>";
*/
class MessageBody {
    private $body = array(
        'text' => "<xml>
   <ToUserName><![CDATA[(@to@)]]></ToUserName>
   <FromUserName><![CDATA[(@from@)]]></FromUserName> 
   <CreateTime>(@time@)</CreateTime>
   <MsgType><![CDATA[text]]></MsgType>
   <Content><![CDATA[(@text@)]]></Content>
</xml>",
        'image' => '<xml>
   <ToUserName><![CDATA[(@to@)]]></ToUserName>
   <FromUserName><![CDATA[(@from@)]]></FromUserName>
   <CreateTime>(@time@)</CreateTime>
   <MsgType><![CDATA[image]]></MsgType>
   <Image>
       <MediaId><![CDATA[(@media_id@)]]></MediaId>
   </Image>
</xml>',
        'voice' => '<xml>
   <ToUserName><![CDATA[(@to@)]]></ToUserName>
   <FromUserName><![CDATA[(@from@)]]></FromUserName>
   <CreateTime>(@time@)</CreateTime>
   <MsgType><![CDATA[voice]]></MsgType>
   <Voice>
       <MediaId><![CDATA[(@media_id@)]]></MediaId>
   </Voice>
</xml>',
        'video' => '<xml>
   <ToUserName><![CDATA[(@to@)]]></ToUserName>
   <FromUserName><![CDATA[(@from@)]]></FromUserName>
   <CreateTime>(@time@)</CreateTime>
   <MsgType><![CDATA[video]]></MsgType>
   <Video>
       <MediaId><![CDATA[(@media_id@)]]></MediaId>
       <Title><![CDATA[(@title@)]]></Title>
       <Description><![CDATA[(@description@)]]></Description>
   </Video>
</xml>',
        'news' => '<xml>
   <ToUserName><![CDATA[(@to@)]]></ToUserName>
   <FromUserName><![CDATA[(@from@)]]></FromUserName>
   <CreateTime>(@time@)</CreateTime>
   <MsgType><![CDATA[news]]></MsgType>
   <ArticleCount>(@count@)</ArticleCount>
   <Articles>
(@items@)   </Articles>
</xml>',
        'items' => '       <item>
           <Title><![CDATA[(@title@)]]></Title> 
           <Description><![CDATA[(@description@)]]></Description>
           <PicUrl><![CDATA[(@picurl@)]]></PicUrl>
           <Url><![CDATA[(@url@)]]></Url>
       </item>
',
    );
    
    public function GeneralMessage($ary,$type = 'text') {
        $type = strtolower($type);
        if (!isset($this->body[$type])) return '';
        return $this->parsebody($this->body[$type],$ary);
    }
    
    private function parsebody($template,$ary) {
        $ret = $template;
        if (is_array($ary)) {
            if (!isset($ary['time'])) {
                $ret = preg_replace("/\\(@time@\\)/i",time(),$ret);
            }
            foreach ($ary as $k => $v) {
                if (!is_array($v)) {
                    $p = "/\\(@{$k}@\\)/i";
                    $ret = preg_replace($p,$v,$ret);
                }
            }
            if (isset($ary['items']) && is_array($ary['items'])) {
                $count = count($ary['items']);
                $ret = preg_replace('/\(@count@\)/i',$count,$ret);
                $items = '';
                foreach ($ary['items'] as $n) {
                    $items .= $this->parsebody($this->body['items'],$n);
                }
                $ret = preg_replace('/\(@items@\)/i',$items,$ret);
            }
        }
        $ret = preg_replace('/\(@.*?@\)/','',$ret);
        return $ret;
    }
}
}
?>
